﻿using System;
using System.Windows.Forms;
using OrionApplication.OrionLib;
using OrionApplication.OrionLib.Dao;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionForm
{
    public partial class OrionEvenForm : OrionBaseDoubleForm
    {
        public OrionEvenForm()
            : base()

        {
            dao = new DaoOrionEven();
            InitializeComponent();
        }

        public OrionEvenForm(String titre)
            : base(titre)
        {
            dao = new DaoOrionEven();
            InitializeComponent();
        }

        private void DupliqueEnregistrement()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                if (OrionOutils.ChoixOuiNon(OrionConfBase.ConfirmDuplique) == DialogResult.Yes)
                {
                    OrionEven oe = ((DaoOrionEven)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                    oe = ((DaoOrionEven)dao).Insert(oe);
                    this.RempliKDgv();
                    this.FocusSur(oe.OrionId);
                }
            }
            else
                OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
        }

        private void RempliForm(OrionEven oe)
        {
            kTbFormulaire.Text = oe.FormName;
            kTbChamp.Text = oe.ChampName;
            kTbQuoi.Text = oe.Quoi;
            kCbTypeEven.Cb.SelectedValue = oe.TypeEvenId;
        }

        private void SauvegardeForm()
        {
            OrionEven oe = this.RempliObject();
            if (kLblMode.Text == OrionConfBase.OrionModeInsert)
                oe = ((DaoOrionEven)dao).Insert(oe);
            else
            {
                if (kDgv.SelectedRows.Count > 0)
                {
                    oe.OrionId = Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value);
                    oe = ((DaoOrionEven)dao).Update(oe);

                }
                else
                    OrionOutils.Alerte(OrionConfBase.AucuneLigneSelectionnee);
            }
            this.RempliKDgv();
            this.FocusSur(oe.OrionId);
        }

        private OrionEven RempliObject()
        {
            OrionEven retour = new OrionEven();
            retour.FormName = kTbFormulaire.Text;
            retour.ChampName = kTbChamp.Text;
            retour.Quoi = kTbQuoi.Text;
            retour.TypeEvenId = Convert.ToInt32(kCbTypeEven.Cb.SelectedValue);
            return retour;
        }

        public void RowEnter()
        {
            if (kDgv.SelectedRows.Count > 0)
            {
                OrionEven oe = ((DaoOrionEven)dao).SelectById(Convert.ToInt32(kDgv.SelectedRows[0].Cells["OrionId"].Value));
                this.RempliForm(oe);
            }
        }

        private void btnSpecGoTo_Click(object sender, EventArgs e)
        {
            this.GoToOrionTable("TYPEEVEN", "Gestionnaire des Types d'Evènements");
        }
    }
}
